<?php
namespace Home\Controller;
class AppointmentController extends ComController{
	//在线预约列表页
	public function index(){
		$city = intval($_GET['city'])?$_GET['city']:'410100';
		$city_name=$_GET['city_name']?$_GET['city_name']:'郑州市';
		$date_c = I('get.date','','strip_tags');
		if(!empty($date_c)){
			$date = strtotime($date_c);
		}else{
			$date_c = date('Y-m-d');
			$date = strtotime($date_c);
		}
		$where="city=$city and time = $date";
		$app_date = M('appointment_date');
		//分页
		$p= intval($_GET['p'])?$_GET['p']:1;
		$pagesize = 5;//每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$sql = $app_date->where($where)->group("app_co_id")->select();
		//var_dump($sql);
		$count=count($sql);
		//var_dump($count);
		//die;
		$num=ceil($count/$pagesize);
		$this->assign('num',$num);
		$this->assign('time',$date_c);
		$this->assign('city',$city);
		$this->assign('city_name',$city_name);
		$this->display();
		
	}
	

	//预约学车分页实现
	public function  info(){
		//根据日期查教练排班(预约学车)
		$date_c = I('post.date','','strip_tags');
		$city = $_POST['city']?$_POST['city']:'410100';
		$city_name=$_POST['city_name']?$_POST['city_name']:'郑州市';
		//分页
		$p= intval($_POST['p'])?$_POST['p']:1;
		$pagesize =5;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		//var_dump($p);die;
		
		if(!empty($date_c)){
			$date = strtotime($date_c);
		}else{
			$date_c = date('Y-m-d');
			$date = strtotime($date_c);
		}
		$this->assign('time',$date_c);
		$this->assign('city',$city);
		$this->assign('city_name',$city_name);
		$prefix = C('DB_PREFIX');
		
		//获取今天的时间戳  到日期
		$time_d = date('Y-m-d');
		$time_s = strtotime($time_d);
		$hours = date('H');
		//var_dump($time_s);
		//var_dump($hours);
		

		//根据日期查教练排班
		if(!empty($date)){
			$app_date = M('appointment_date');
			//分页
			
			$c = $app_date
			->where("time = $date and city=$city")
			->group("app_co_id")
			->limit($offset . ',' . $pagesize)
			->select();
			//var_dump($c);
			if(!empty($c)){
				$app_co_id='';
				foreach ($c as $key=>$val){
					if($app_co_id==''){
						$app_co_id = $val['app_co_id'];
					}else{
						$app_co_id = $app_co_id.','.$val['app_co_id'];
					}
				}
				//var_dump($app_co_id);die;
				
				$res = $app_date
				->alias('d')
				->join("{$prefix}appointment_time as t on d.app_time_id=t.id")
				->field('d.*,t.time as site,t.price')
				->where(" d.city=$city  and d.time = $date and app_co_id in ($app_co_id)")->select();
				//var_dump($res);die;
					
				if(!empty($res)){
					$coa_id = '';
					foreach ($res as $k=>$v){
						if($v['status'] == 1){
						// 				判断时间段是否过期
						if($v['time'] < $time_s){
							$res[$k]['status']= '2';//过期状态
						}
						if($v['time'] == $time_s){
							$h=strstr($v['site'], ':', TRUE);
							if($h <=$hours){
								$res[$k]['status']= '2';//过期状态
							}
						}
						}elseif($v['status'] == 0){
							unset($res[$k]);
						}
							
						//将app_co_id 取出，查询教练信息
						if($coa_id == ''){
							$coa_id = $v['app_co_id'];
						}else{
							$coa_id= $coa_id.','.$v['app_co_id'];
						}
						
					}
					//根据教练id号 查找教练信息
					if(!empty($coa_id)){
						$app_coach = M('appointment_coach');
						$app_record = $app_coach
						->alias('c')
						->join("{$prefix}license_type as t on c.li_type=t.id")
						->join("{$prefix}site as s on c.site_id=s.id")
						->join("{$prefix}car as ca on c.car_id=ca.id")
						->join("{$prefix}coach as co on c.coach_id=co.id ")
						->where("c.id in ($coa_id)")
						->field('c.*,t.type,s.site,ca.car_num,co.img')
						->select();//教练员的基本信息
		
						$this->assign('info',$app_record);
						$this->assign('record',$res);
					}else{
						// 					未拿到教练id
					}
				}else{
					//未找到教练排班信息
				}
					
			}else{
				//分页   没有数据
			}
		}else{
			//未转递日期参数
		}
		//var_dump($app_record);
		//var_dump($res);
		//die;
		$this->display();
	}
	
	
	
	//从驾校进入
	public  function  sc_app(){
		//接收驾校id
		$sc_id =I('get.school_id','','intval');
		$date_c = I('get.date','','strip_tags');
		if(!empty($date_c)){
			$date = strtotime($date_c);
		}else{
			$date_c = date('Y-m-d');
			$date = strtotime($date_c);
		}
		$app_date = M('appointment_date');
		//分页
		$p= intval($_GET['p'])?$_GET['p']:1;
		$pagesize = 5;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$sql = $app_date->where(" school_id = $sc_id and time = $date")->group("app_co_id")->select();
		$count=count($sql);
		$num=ceil($count/$pagesize);
		
		/* var_dump($num);
		var_dump($date_c);
		var_dump($sc_id);die; */
		
		$this->assign('num',$num);
		$this->assign('time',$date_c);
		$this->assign('sc_id',$sc_id);
		$this->display();
		
	}
	
	
	//sc
	public function sc_info(){
		//根据日期查教练排班(预约学车)
		$date_c = I('post.date','','strip_tags');
		if(!empty($date_c)){
			$date = strtotime($date_c);
		}else{
			$date_c = date('Y-m-d');
			$date = strtotime($date_c);
		}
		$this->assign('time',$date_c);

// 		接收驾校id
		$sc_id =I('post.school_id','','intval');
		//$sc_id = 1;
		$prefix = C('DB_PREFIX');
		
		//获取今天的时间戳  到日期
		$time_d = date('Y-m-d');
		$time_s = strtotime($time_d);
		$hours = date('H');

		
		//分页
		$p= intval($_POST['p'])?$_POST['p']:1;
		
		$pagesize = 5;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
// 		驾校id
		if(!empty($sc_id)){
		//根据日期查教练排班
		if(!empty($date)){
			$app_date = M('appointment_date');
			//分页
			$count = $app_date->where("time = $date and school_id = $sc_id")->group("app_co_id")->count();
			$c = $app_date
			->where("time = $date and school_id = $sc_id")
			->group("app_co_id")
			->limit($offset . ',' . $pagesize)
			->select();
			if(!empty($c)){
				$app_co_id='';
				foreach ($c as $key=>$val){
					if($app_co_id==''){
						$app_co_id = $val['app_co_id'];
					}else{
						$app_co_id = $app_co_id.','.$val['app_co_id'];
					}
				}
		
				$res = $app_date
				->alias('d')
				->join("{$prefix}appointment_time as t on d.app_time_id=t.id")
				->field('d.*,t.time as site,t.price')
				->where(" d.school_id = $sc_id and d.time = $date and app_co_id in ($app_co_id)")->select();
					
				if(!empty($res)){
					$coa_id = '';
					foreach ($res as $k=>$v){
						
					if($v['status'] == 1){
						// 				判断时间段是否过期
						if($v['time'] < $time_s){
							$res[$k]['status']= '2';//过期状态
						}
						if($v['time'] == $time_s){
							$h=strstr($v['site'], ':', TRUE);
							if($h <=$hours){
								$res[$k]['status']= '2';//过期状态
							}
						}
						}elseif($v['status'] == 0){
							unset($res[$k]);
						}
						
							
						//将app_co_id 取出，查询教练信息
						if($coa_id == ''){
							$coa_id = $v['app_co_id'];
						}else{
							$coa_id= $coa_id.','.$v['app_co_id'];
						}
						//
					}
					//根据教练id号 查找教练信息
					if(!empty($coa_id)){
						$app_coach = M('appointment_coach');
						$app_record = $app_coach
						->alias('c')
						->join("{$prefix}license_type as t on c.li_type=t.id")
						->join("{$prefix}site as s on c.site_id=s.id")
						->join("{$prefix}car as ca on c.car_id=ca.id")
						->join("{$prefix}coach as co on c.coach_id=co.id ")
						->where("c.id in ($coa_id)")
						->field('c.*,t.type,s.site,ca.car_num,co.img')
						->select();//教练员的基本信息
						$this->assign('info',$app_record);
						$this->assign('record',$res);
						//var_dump($app_coach->getLastSql());
					}else{
						// 					未拿到教练id
					}
				}else{
					//未找到教练排班信息
				}
					
			}else{
				//分页   没有数据
			}
		}else{
			//未转递日期参数
		}
	}else{
			
	}
		
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('page',$page);
		//var_dump($app_record);
		//var_dump($res);
		$this->display();
	}
}